<!--
 * @Author: your name
 * @Date: 2021-05-31 21:39:34
 * @LastEditTime: 2021-07-10 01:04:30
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \JS高级学习\js\base\13.html
-->
<!doctype html>
<html>

<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
  <title>覆盖物的添加与移除</title>
  <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
  <!-- <script src="https://cache.amap.com/lbs/static/es5.min.js"></script> -->
  <script src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script>
  <!-- <script type="text/javascript" src="https://cache.amap.com/lbs/static/addToolbar.js"></script> -->
  <style>
    html,
    body,
    #container {
      width: 100%;
      height: 100%;
    }

    label {
      width: 55px;
      height: 26px;
      line-height: 26px;
      margin-bottom: 0;
    }

    button.btn {
      width: 80px;
    }
  </style>
</head>

<body>
  <div id="container"></div>

  <div class="input-card" style="width:24rem;">
    <h4>添加、删除覆盖物</h4>
    <div class="input-item">
      <label>Marker：</label>
      <button class="btn" id="add-marker" style="margin-right:1rem;">添加Marker</button>
      <button class="btn" id="remove-marker">删除Marker</button>
    </div>
    <div class="input-item">
      <label>Circle：</label>
      <button class="btn" id="add-circle" style="margin-right:1rem;">添加Circle</button>
      <button class="btn" id="remove-circle">删除Circle</button>
    </div>
  </div>

  <script>
    var map = new AMap.Map('container', {
      resizeEnable: true,
      zoom: 11,
      center: [116.397428, 39.90923]
    });
    // 构造点标记
    var marker = new AMap.Marker({
      icon: "https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png",
      position: [116.405467, 39.907761]
    });
    // 构造矢量圆形
    var circle = new AMap.Circle({
      center: new AMap.LngLat("116.403322", "39.920255"), // 圆心位置
      radius: 1000,  //半径
      strokeColor: "#F33",  //线颜色
      strokeOpacity: 1,  //线透明度
      strokeWeight: 3,  //线粗细度
      fillColor: "#ee2200",  //填充颜色
      fillOpacity: 0.35 //填充透明度
    });

    // new AMap.Polyline()
    //事件绑定
    document.querySelector("#add-marker").onclick = function () {
      map.add(marker);
      map.setFitView();
    }
    document.querySelector("#remove-marker").onclick = function () {
      map.remove(marker);
      map.setFitView();
    }
    document.querySelector("#add-circle").onclick = function () {
      map.add(circle);
      map.setFitView();
    }
    document.querySelector("#remove-circle").onclick = function () {
      map.remove(circle);
      map.setFitView();
    }

    function Polyline(map, PolylineArr, paths) {
      function getPathArr(v) {
        let pathArr = []
        v.map(item => {
          pathArr.push([item.location.longitude, item.location.latitude])
        })
      }
      PolylineArr.map(item => map.remove(item))
      PolylineArr.length ? PolylineArr.length = 0 : ''
      paths.map(value => {
        let Polyline = new AMap.Polyline({
          path: paths,
          strokeColor: "#F33",
          strokeOpacity: 1,
          strokeWeight: 3,
          fillColor: "#ee2200",
          fillOpacity: 0.35
        });
        PolylineArr.push(Polyline)
      })
      PolylineArr.map(item => map.add(item))
    }


    let arr = [11, 22, 33, 44, 50, 66, 77, 88, 90, 100, 110, 115]
    function splitArr(arr) {
      const pathArr = []
      const lineArr = []
      arr.reduce((curr, next, index) => {
        if (next - curr > 9) {
          lineArr.push(curr)
        } else {
          lineArr.push(curr)
          pathArr.push(lineArr);
          lineArr.length = 0;
        }
        if (index === arr.length - 1) lineArr.push(next)
        return next
      })
      if (lineArr.length) pathArr.push(lineArr);
    }
    splitArr(arr)



    let pathArr = []
    let lineArr = []
    for (let i = 0; i < arr.length; i++) {
      if (lineArr.length === 0) {
        lineArr.push(arr[i])
      } else {
        if (arr[i] - arr[i - 1] > 9) {
          lineArr.push(arr[i])
        } else {
          pathArr.push(lineArr);
          lineArr = []
          i--
        }
      }

    }
    if (lineArr.length) pathArr.push(lineArr);
    console.log(pathArr);


    async function sleep(ms = 1000) {
      return new Promise(resolve => {
        // setTimeout(resolve, ms);
        setTimeout(() => {
          resolve()
        }, ms);

      });
    }
    async function run() {
      for (const value of [1, 2, 3, 4, 5, 6]) {
        await sleep();
        console.log(value);
      }
    }
    // run();




    async function a() {
      for (let i = 6; i > -1; i--) {
        await new Promise(resolve => {
          setTimeout(resolve, 1000)
        });
        console.log(i);
      }
    }
    a()
  </script>
</body>

</html>